var page = 1;//页数
var total = 0;//总条数
var totalPage = 0;//总页数
var clickName="法学类";//默认设置打开列表页面为法学类
$(function () {
    //立法专家库
    var option1 = {

        title: {
            text: ''
        },
        tooltip: {
            trigger: 'item',
            formatter: "{b} : {c} 件(占比{d}%)",
            textStyle:{
                align:'left'
            },
        },
        color: [ '#1980FF', '#FF635C', '#9083F5', '#FCC300','#14C0C2'],
        legend: {
            orient: 'vertical',
            show: true,
            width:600,              // 宽度
            itemGap: 10,            // 间隔
            itemWidth: 10,          // 图形宽度。
            itemHeight: 10,
            bottom: '10%',

            left: 'center',
            // data:[],
            formatter: [
                '{a|{name}}'+'{b|}'
            ].join('\n'),//图例换行对齐
            textStyle: {
                rich: {
                    a: {
                        width: 55,
                        // color: '#909399',
                        fontSize: 12,
                        lineHeight: 12
                    }
                },
            },
        },
        series: [{
            name: '立法专家占比',
            type: 'pie',
            center: ['50%', '32%'],
            radius:'55%',
            selectedMode: 'single',
            avoidLabelOverlap: false,
            label: {
                normal:{
                    show:false,
                },
                // show: true
            },
            emphasis: {
                label: {
                    show: false,//图上说明开关
                }
            },
            labelLine: {
                show: false,//图上连线开关
                length:30,
                length2:15,
                smooth:false,
                minTurnAngle:90,
                maxSurfaceAngle:90
            },
            data: [
                {
                    name: '法学类',
                    value: 50.8,
                    selected: true
                },{
                    name: '经济文化类',
                    value: 17,
                },{
                    name: '政治管理类',
                    value: 15,
                }, {
                    name: '理工科类',
                    value: 15,
                },{
                    name: '语言文字类',
                    value: 2.2,
                }]
        }]
    };
    var myChart1 = echarts.init(document.getElementById("chart"));
    myChart1.setOption(option1);
    // 绑定点击事件
    myChart1.on("click", clickFunc);

// 事件处理函数
// param ： echarts的事件处理函数自带的参数，包含了本次触发对象的所有相关参数
    function clickFunc(param) {
        total=0;
        page=1;
        totalPage=0
        clickName=param.data.name;
        setList(page,clickName);
        // alert(param.data.name); // 当前点击对象的name
    }
    window.addEventListener("resize",function(){//适应div大小
        myChart1.resize();
    });

    setList (page,clickName);

    $("#pre").click(function () {
        if (page == parseInt(total/10)+1){
            $("#next").attr("disabled",false)
        }
        page = page-1;
        if (page<=0){page=1};
        setList(page,clickName);
    });

    $("#next").click(function () {
        if (page == 1){
            $("#pre").attr("disabled",false)
        }
        page = page+1;
        if (page > (total/10)+1){page = (total/10)+1};
        setList(page,clickName);

    });
});
//列表数据切换方法
function setList(index,e) {
    page=index;
    $(".table_data").html("");
    var html="";
    total=0;
    html = `<tr>
                <th width="10%">序号</th>
                <th width="10%">姓名</th>
                <th width="10%">性别</th>
                <th width="30%">研究领域/业务专长</th>
                <th width="25%">工作单位</th>
                <th width="15%">职务/职称</th>
            </tr>`; //表格标题
    $.ajax({
        url:'./js/digitalPlatform/legislativeExpert.json',
        type: 'get',
        dataType: 'JSON',
        async: false,
        success:function init (data) {
            var list=[];
            $.each(data,function (i,value) {
                var pram=e;
                var sort="";
                sort=value.sort
                if (sort==pram){
                    list.push(value);

                }
            });
            $.each(list,function (index,val) {
                total = total+1;

                if (index >= (page-1)*10 && index<10*page){
                    html+=`<tr>
                <td class="align_center" width="100">${index+1}</td>
                <td class="align_center" width="400">${val.name}</td>
                <td class="align_center" width="500">${val.sex}</td>
                <td class="align_center" width="200">${val.specializedSkill}</td>
                <td class="align_center" width="300">${val.unit}</td>
                <td class="align_center" width="400">${val.duty}</td>
                
            </tr>`
                }
            });
            totalPage = parseInt(total/10+1);
            $(".table_data").html(html);
            $("#tabTitle").html(clickName);
            if (page == 1){
                $("#pre").attr("disabled",true)
            }else {
                $("#pre").attr("disabled",false)
            }
            if (page == parseInt(total/10)+1){
                $("#next").attr("disabled",true)
            }else {
                $("#next").attr("disabled",false)
            };
            pageLoad();
            // console.log(parseInt(total/15)+1)

        }
    });
};


function pageLoad() {
    // var dqPage = $("#dqPage").text();//得到当前页数
    // dqPage = parseInt(dqPage);//得到的文本转成int
    // var pageCount = $("#pageCount").text();//得到总页数
    // pageCount = parseInt(pageCount);
    var i = 1;
    i = parseInt(i);
    var item="";
    if (totalPage <= 3 ) {//总页数小于五页，则加载所有页

        for (i; i <= totalPage; i++) {
            if (i == page) {
                item += "<span class='disabled'>"+i+"</span>";
            }else{
                item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
            }
        };
        $('#pageBtn').html(item);
        return;
    }else if (totalPage > 3) {//总页数大于五页，则加载五页
        if (page < 3) {//当前页小于5，加载1-5页
            for (i; i <= 3; i++) {
                if (i == page) {
                    item += "<span class='disabled'>"+i+"</span>";
                }else{
                    item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
                }
            };
            if (page <= totalPage-2) {//最后一页追加“...”代表省略的页
                item += "<span> . . . </span>";
            }
            $('#pageBtn').html(item);
            return;
        }else if (page >= 3) {//当前页大于5页
            for (i; i <= 1; i++) {//1,2页码始终显示
                item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
            }
            item += "<span> . . . </span>";//2页码后面用...代替部分未显示的页码
            if (page+1 == totalPage) {//当前页+1等于总页码
                for(i = page-1; i <= totalPage; i++){//“...”后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
                    }
                }
            }else if (page == totalPage) {//当前页数等于总页数则是最后一页页码显示在最后
                for(i = page-2; i <= totalPage; i++){//...后面跟三个页码当前页居中显示
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
                    }
                }
            }else{//当前页小于总页数，则最后一页后面跟...
                for(i = page-1; i <= page+1; i++){//page+1页后面...
                    if (i == page) {
                        item += "<span class='disabled'>"+i+"</span>";
                    }else{
                        item += "<span class='show' onclick='setList("+i+",clickName)'>"+i+"</span>";
                    }
                }
                item += "<span> . . . </span>";
            }
            $('#pageBtn').html(item);
            return;
        }
        // console.log(item);
    }

};